This
first application is a program that can be pretty self-sufficient, but
not applications are like that. Most applications will want to interact
with the phone’s operation system to work with other parts of the
phone. From within your application you may want to make a phone call,
interact with the user’s contacts, take a pictures, etc. The Windows
Phone 7 SDK calls these types of interactions Tasks. Tasks let you
leave an application (and optionally return) to perform a number of
phone-specific tasks. Some of the most common of these are:
CameraCaptureTask
EmailAddressChooserTask
EmailComposeTask
PhoneCallTask
SearchTask
WebBrowserTask
These tasks allow you to launch a task for the user to do. In some of these tasks (e.g. CameraCaptureTask, EmailAddressChooserTask), once the task is complete the user expects to return to your application; while in others (e.g. SearchTask) the user may be navigating to a new activity (and may come back via the back key, but may not).
Let’s start with a simple task, the SearchTask. In our code, create an event handler for the MouseLeftButtonUp event on theEllipse. Then inside the handler for the MouseLeftButtonUp event, you can create an instance of the SearchTask, set the search criteria and call Show to launch the task:
...
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
...
theEllipse.MouseLeftButtonUp += new
MouseButtonEventHandler(theEllipse_MouseLeftButtonUp);
}
void theEllipse_MouseLeftButtonUp(object sender,
MouseButtonEventArgs e)
{
SearchTask task = new SearchTask();
task.SearchQuery = "Windows Phone 7";
task.Show();
}
...
}
If you run your application,
you’ll see that when you tap on theEllipse it will launch the Phone’s
Search using the Search Query that you supplied (as shown in Figure 1).
While this sort of simple
task is useful, the more interesting story is being able to call tasks
that return to your application. For example, let’s pick an e-mail
address from the phone and show it in our application. The big
challenge here is when we launch our application, we may get tombstoned
(or deactivated). Remember that on the phone, only one application can
be running at a time. In order to have our task wired up when our
application is activated (remember, it can be deactivated or even
unloaded if necessary), we have to have our task at the page or
application level and wired up during construction. So in our page,
create a class level field and wire up the Completed event at the end
of the constructor for it like so:
public partial class MainPage : PhoneApplicationPage
{
EmailAddressChooserTask emailChooser = new EmailAddressChooserTask();
// Constructor
public MainPage()
{
...
emailChooser.Completed += new
EventHandler<EmailResult>(emailChooser_Completed);
}
...
}
In the event handler, we can simply show the e-mail chosen using the MessageBox API:
...
void emailChooser_Completed(object sender, EmailResult e)
{
MessageBox.Show(e.Email);
}
...
Now we need to call the
chooser. To do this, let’s highjack the event that gets called when
theEllipse is tapped. Just comment out the old SearchTask code and add
a call to the emailChooser’s Show method like so:
...
void theEllipse_MouseLeftButtonUp(object sender,
MouseButtonEventArgs e)
{
//SearchTask task = new SearchTask();
//task.SearchQuery = "Windows Phone 7";
//task.Show();
// Get an e-mail from the user's Contacts
emailChooser.Show();
}
...
Once you run the
application, a list of contacts will be shown (if you have any) and let
you pick a contact (and which address if there is more than one) as
shown in Figure 2.
When the chooser is
launched, you will notice that the debugger stops in Visual Studio.
This is expected as our application is being deactivated (tombstoned)
so the debugger no longer has a process to hold on to. Once the user
picks the contact, the phone returns to your application. You will
notice the emulator shows a black screen. This is your cue (you have 10
seconds) to relaunch the app so you can re-active your application.
Once you’ve relaunched your application, your debugging will continue
where you left off. The event handler should be called when it is
returned to the application as shown in Figure 3.